package com.jt.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jt.mapper.CartMapper;
import com.jt.pojo.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DubboCartServiceImpl implements DubboCartService{

    @Autowired
    private CartMapper cartMapper;

    @Override
    public List<Cart> findCartListByUserId(Long userId) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return cartMapper.selectList(queryWrapper);
    }

    //update tb_cart set num = #{num},updated = #{updated} where user_id =#{userId} and item_id = #{itemId}
    @Override
    @Transactional
    public void updateCartNum(Cart cart) {
        Cart cartTemp = new Cart();
        cartTemp.setNum(cart.getNum())
                .setUpdated(new Date());  //更新时间
        UpdateWrapper<Cart> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("item_id",cart.getItemId())
                .eq("user_id",cart.getUserId());
        //根据对象中不为null的属性，设定set条件
        cartMapper.update(cartTemp,updateWrapper);
    }

    /**
     *1.第一次架构  入库
     * 2.第二次架构 更新数据库
     * @param cart
     */
    @Override
    @Transactional
    public void saveCart(Cart cart) {
        //1.先查询数据库职工是否有该记录  itemId和userId
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",cart.getUserId());
        queryWrapper.eq("item_id",cart.getItemId());
        Cart cartDB = cartMapper.selectOne(queryWrapper);
        //cartDB几乎所有的数据都不为null  将来可能被当作条件使用，所以并不合适
        if (cartDB == null){
            //1.说明第一次
            cart.setCreated(new Date())
                    .setUpdated(cart.getCreated());
            cartMapper.insert(cart);
        }else {
            //只更新商品数量
            int num = cart.getNum() + cartDB.getNum();
            Cart cartTemp = new Cart();
            cartTemp.setId(cartDB.getId())
                    .setNum(num)
                    .setUpdated(new Date());
            cartMapper.updateById(cartTemp);//根据主键更新数据库。

//            cartMapper.updateCatNum(cartDB.getId(),num,new Date());
        }
    }

    @Override
    public void deleteCart(Cart cart) {
//        cartMapper.deleteById(itemId);//通过主键的方式进行删除
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>(cart);
        cartMapper.delete(queryWrapper);
    }
}
